﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using DBUtility;
using IDAL;
using Model;

namespace DAL
{
    public class ProductType : BaseRepository<ProductTypeInfo>, IProductType
    {
        #region 静态变量部分
        private const string PARM_PRODUCTTYPE_ID = "@ProductTypeID";
        private const string PARM_PRODUCTTYPE_CUSTOMERNAME = "@CustomerName";
        private const string PARM_PRODUCTTYPE_NAME = "@ProductTypeName";
        private const string PARM_PRODUCTTYPE_ISPASSIVE = "@IsPassive";
        private const string PARM_PRODUCTTYPE_SN3BK = "@SN3BK";
        private const string PARM_PRODUCTTYPE_MATCHINGSN = "@MatchingSN";
        private const string PARM_PRODUCTTYPE_HOUSEAMOUNT = "@HouseAmount";
        #endregion

        #region 数据检索部分
        public IList<ProductTypeInfo> LoadEntities()
        {
            return ExcuteSqlToGetEntities("ProductType_select");
        }

        /// <summary>
        /// Get an individual category based on a provided id
        /// </summary>
        /// <param name="categoryId">Category id</param>
        /// <returns>Details about the Category</returns>
        public ProductTypeInfo LoadEntity<T1>(T1 productTypeId)
        {
            SqlParameter parm = GetParameter();
            //Bind the parameter
            parm.Value = productTypeId;
            //return default(ProductTypeInfo);                                                           //针对泛型的这种语法做个标记
            return ExcuteSqlToGetEntity("ProductType_selectbyID", parm);
        }

        public ProductTypeInfo GetProductTypebyName(string productTypeName)
        {
            SqlParameter parm = new SqlParameter(PARM_PRODUCTTYPE_NAME, SqlDbType.NVarChar, 50);
            parm.Value = productTypeName;
            return ExcuteSqlToGetEntity("ProductType_selectbyName", parm);
        }

        public IList<ProductTypeInfo> GetProductTypesbyCustomerID(string customerID)
        {
            SqlParameter parm = new SqlParameter(PARM_PRODUCTTYPE_CUSTOMERNAME, SqlDbType.NVarChar, 50);
            parm.Value = customerID;
            return ExcuteSqlToGetEntities("ProductType_selectByCustomerID", parm);
        }
        #endregion

        #region 注释掉的部分
        //private static ProductTypeInfo ExcuteSqlToGetProductType(string spname, params SqlParameter[] parm)
        //{
        //    ProductTypeInfo productType = null;
        //    using (SqlDataReader rdr = sqlHelper.ExecuteReader(sqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, spname, parm))
        //    {
        //        if (rdr.Read())
        //            productType = GetEntityRecord(rdr);
        //        else
        //            productType = new ProductTypeInfo();
        //    }
        //    return productType;
        //}

        ////重构，把读取SqlDataReader,生成IList<ProductTypeInfo>的代码抽取出来
        //private static IList<ProductTypeInfo> ExcuteSqlToGetProductTypes(string spname, params SqlParameter[] parm)
        //{
        //    IList<ProductTypeInfo> productTypes = new List<ProductTypeInfo>();
        //    using (SqlDataReader rdr = sqlHelper.ExecuteReader(sqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, spname, parm))
        //    {
        //        while (rdr.Read())
        //        {
        //            ProductTypeInfo pro = GetEntityRecord(rdr);
        //            productTypes.Add(pro);
        //        }
        //    }
        //    return productTypes;
        //}

        ///弃用函数
        //public override void Insert(ProductTypeInfo productType)
        //{
        //    SqlParameter[] productTypeParms = GetParameters();
        //    //Bind the parameter
        //    productTypeParms[0].Value = productType.ProductTypeID;
        //    productTypeParms[1].Value = productType.CustomerID;
        //    productTypeParms[2].Value = productType.ProductTypeName;
        //    productTypeParms[3].Value = productType.IsPassive;
        //    productTypeParms[4].Value = productType.SN3BK;
        //    productTypeParms[5].Value = productType.MatchingSN;
        //    SqlCommand cmd = new SqlCommand();
        //    foreach (SqlParameter parm in productTypeParms)
        //        cmd.Parameters.Add(parm);
        //    using (SqlConnection conn = new SqlConnection(sqlHelper.ConnectionStringLocalTransaction))
        //    {
        //        conn.Open();
        //        cmd.Connection = conn;
        //        cmd.CommandType = CommandType.StoredProcedure;
        //        cmd.CommandText = "ProductType_Insert";
        //        using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { }
        //    }
        //}

        //public void Update(ProductTypeInfo productType)
        //{
        //    SqlParameter[] productTypeParms = GetParameters();
        //    //Bind the parameter
        //    productTypeParms[0].Value = productType.ProductTypeID;
        //    productTypeParms[1].Value = productType.CustomerID;
        //    productTypeParms[2].Value = productType.ProductTypeName;
        //    productTypeParms[3].Value = productType.IsPassive;
        //    productTypeParms[4].Value = productType.SN3BK;
        //    productTypeParms[5].Value = productType.MatchingSN;
        //    SqlCommand cmd = new SqlCommand();
        //    foreach (SqlParameter parm in productTypeParms)
        //        cmd.Parameters.Add(parm);
        //    using (SqlConnection conn = new SqlConnection(sqlHelper.ConnectionStringLocalTransaction))
        //    {
        //        conn.Open();
        //        cmd.Connection = conn;
        //        cmd.CommandType = CommandType.StoredProcedure;
        //        cmd.CommandText = "ProductType_Update";
        //        using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { }
        //    }
        //}

        //public void Delete(string productTypeId)
        //{
        //    //Create a parameter    
        //    SqlParameter parm = new SqlParameter(PARM_PRODUCTTYPE_ID, SqlDbType.NVarChar, 50);
        //    //Bind the parameter
        //    parm.Value = productTypeId;
        //    SqlCommand cmd = new SqlCommand();
        //    cmd.Parameters.Add(parm);
        //    using (SqlConnection conn = new SqlConnection(sqlHelper.ConnectionStringLocalTransaction))
        //    {
        //        conn.Open();
        //        cmd.Connection = conn;
        //        cmd.CommandType = CommandType.StoredProcedure;
        //        cmd.CommandText = "ProductType_Delete";
        //        cmd.ExecuteNonQuery();
        //    }
        //} 
        #endregion

        #region 重写基类部分
        //-------------------------------------重写超类基础操作---------------------------------------------------------

        public override ProductTypeInfo GetEntityRecord(SqlDataReader rdr)
        {
            ProductTypeInfo pro = new ProductTypeInfo(rdr.GetValue(0).ToString(), rdr.GetValue(1).ToString(), rdr.GetValue(2).ToString(), rdr.GetValue(3).ToString(), rdr.GetValue(4).ToString(), rdr.GetValue(5).ToString(), rdr.GetInt32(6));
            return pro;
        }

        public override ProductTypeInfo GetEntityRecord()
        {
            ProductTypeInfo pro = new ProductTypeInfo();
            return pro;
        }

        public override string GetInsertsp()
        {
            return "ProductType_Insert";
        }

        public override string GetUpdatesp()
        {
            return "ProductType_Update";
        }

        public override string GetDeletesp()
        {
            return "ProductType_Delete";
        }

        public override SqlParameter GetParameter()
        {
            SqlParameter Parm = new SqlParameter(PARM_PRODUCTTYPE_ID, SqlDbType.NVarChar, 50);
            return Parm;
        }

        public override SqlParameter[] GetParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
					new SqlParameter(PARM_PRODUCTTYPE_ID, SqlDbType.NVarChar, 50),
					new SqlParameter(PARM_PRODUCTTYPE_CUSTOMERNAME, SqlDbType.NVarChar, 50),
                    new SqlParameter(PARM_PRODUCTTYPE_NAME, SqlDbType.NVarChar, 50),
					new SqlParameter(PARM_PRODUCTTYPE_ISPASSIVE, SqlDbType.Char , 10),
                    new SqlParameter(PARM_PRODUCTTYPE_SN3BK,SqlDbType.NVarChar,50),
                    new SqlParameter(PARM_PRODUCTTYPE_MATCHINGSN,SqlDbType.NVarChar,50),
                    new SqlParameter(PARM_PRODUCTTYPE_HOUSEAMOUNT,SqlDbType.Int,4)
                };
            return parms;
        }

        public override void SetParameters(ProductTypeInfo productType, params SqlParameter[] Parms)
        {
            Parms[0].Value = productType.ProductTypeID;
            Parms[1].Value = productType.CustomerName;
            Parms[2].Value = productType.ProductTypeName;
            Parms[3].Value = productType.IsPassive;
            Parms[4].Value = productType.SN3BK;
            Parms[5].Value = productType.MatchingSN;
            Parms[6].Value = productType.HouseAmount;
        }
        #endregion
    }
}
